package org.example.big_data.service.impl;

import org.example.big_data.mapper.CategoryMapper;
import org.example.big_data.pojo.Category;
import org.example.big_data.service.CategoryService;
import org.example.big_data.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    @Override
    //1。新增文章分类接口
    public void add(Category category) {
        category.setCreateTime(LocalDateTime.now());  //设置创建时间
        category.setUpdateTime(LocalDateTime.now());  //设置更新时间
        //获取当前登录用户的id值作为外键
        Map<String, Object> map = ThreadLocalUtil.get();  //取出解析后的载荷，在LoginInterceptor中解析名存入线程变量中
        Integer userid = (Integer) map.get("id");  //获取map中的id值
        category.setCreateUser(userid);  //设置CreateUser属性值
        categoryMapper.add(category);
    }

    @Override
    //2.GET请求：/category获取所有文章分类列表，响应数据中data中为一个列表,查询时需要当前用户Id作为外键
    public List<Category> list() {
        //获取当前登录用户的id值作为外键
        Map<String, Object> map = ThreadLocalUtil.get();  //取出解析后的载荷，在LoginInterceptor中解析名存入线程变量中
        Integer userid = (Integer) map.get("id");  //获取map中的id值
        return categoryMapper.list(userid);
    }

    //3./category/detail根据ID获取文章分类详情，即根据id查询出一整行数据，转化为Category对象
    @Override
    public Category detail(Integer id) {
        return categoryMapper.detail(id);
    }

    @Override
    public void update(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    @Override
    public void delete(Integer id) {
        categoryMapper.delete(id);
    }
}
